package cn.lyn4ever.cataccount.service.impl;

import cn.lyn4ever.cataccount.entity.AnalysisBill;
import cn.lyn4ever.cataccount.entity.Bill;
import cn.lyn4ever.cataccount.entity.Property;
import cn.lyn4ever.cataccount.mapper.BillMapper;
import cn.lyn4ever.cataccount.service.BillService;
import cn.lyn4ever.cataccount.service.PropertyService;
import cn.lyn4ever.common.R;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Bill)表服务实现类
 *
 * @author makejava
 * @since 2019-09-21 16:56:57
 */
@Service("billService")
public class BillServiceImpl implements BillService {

    @Resource
    private BillMapper billMapper;
    @Resource
    private PropertyService propertyService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Bill queryById(Integer id) {
        return null;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Bill> queryAllByLimit(int offset, int limit) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param bill 实例对象
     * @return 实例对象
     */
    @Override
    public Bill insert(Bill bill) {
        return null;
    }

    /**
     * 修改数据
     *
     * @param bill 实例对象
     * @return 实例对象
     */
    @Override
    public Bill update(Bill bill) {
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return false;
    }

    /**
     * 新增一条账单记录
     *
     * @param bill
     * @return
     */
    @Override
    @Transactional
    public R saveBill(Bill bill) {
        int line = billMapper.insertOne(bill);

        //判断账单类型，进行用户财产操作
        R r = propertyService.updateOneByBill(bill);

        if (line > 0 && r.getCode() == "200")
            return r;

        return R.fail();
    }

    /**
     * 查询所有账单，不分页
     *
     * @param bill
     * @return
     */
    @Override
    public R list(Bill bill) {
        System.out.println(JSON.toJSONString(bill));

        //查出来的账单列表
        List<Bill> list = billMapper.list(bill);
        if (null == list || list.size() < 1) {
            return R.fail("没有查询到结果");
        }

        //最终放内容的list
        List<AnalysisBill> analysisBillList = new ArrayList<AnalysisBill>();


        Map<String, List<Bill>> lastMap = new HashMap<>();

        for (Bill bill1 : list) {
            String createtime = bill1.getCreatetime().substring(0, 10);
            if (null == lastMap.get(createtime)) {
                lastMap.put(createtime, new ArrayList<>());
            }

            lastMap.get(createtime).add(bill1);

        }

        AnalysisBill analysisBill = new AnalysisBill();
        //开始统计查询每一天的账单
        for (String time : lastMap.keySet()) {
            bill.setCreatetime(time);
            analysisBill = billMapper.getAnalysisBills(bill);
            analysisBill.setBills(lastMap.get(time));
            analysisBillList.add(analysisBill);
        }


        System.out.println(JSON.toJSONString(analysisBillList));


//        if (null != list && list.size() > 0) {

        return R.sucess("查询成功", analysisBillList);
//        }


    }

    /**
     * 查询单个账单
     *
     * @param bill
     * @return
     */
    @Override
    public R getOne(Bill bill) {
        Bill bill1 = billMapper.getOne(bill);
        System.out.println(JSON.toJSONString(bill1));
        if (null != bill1) {
            return R.sucess("查询成功", bill1);
        }
        return R.fail();
    }

    /**
     * 根据财产id查账单
     *
     * @param property
     * @return
     */
    @Override
    public R getListByProperty(Property property) {
        List<Bill> listByProperty = billMapper.getListByProperty(property);
        if (null != listByProperty && listByProperty.size() > 0) {
            return R.sucess("查询成功", listByProperty);
        }
        return R.fail();
    }

    /**
     * 根据id删除某一个账单
     *
     * @param bill
     * @return
     */
    @Override
    @Transactional
    public R delOne(Bill bill) {
        System.out.println(">>>>>>>>>>>>>>" + JSON.toJSONString(bill));
        R r = R.fail("删除失败");
        bill=billMapper.getOne(bill);
        int line = billMapper.delOne(bill);

        System.out.println(">>>>>>>>>>>>>>" + JSON.toJSONString(bill));
        if (null != bill.getTravel() && !"".equals(bill.getTravel())) {
            JSONArray objects = JSON.parseArray(bill.getTravel());
            int id1 = Integer.parseInt(JSON.parseObject(objects.get(0).toString()).get("propertId").toString());
            bill.setPropertyid(id1);
            bill.setConsumetype("1");//支出，删除时要写反
            System.out.println(">>>>>>abc>>>>>>>>" + JSON.toJSONString(bill));

           r =  propertyService.updateOneByBill(bill);

            int id2 = Integer.parseInt(JSON.parseObject(objects.get(1).toString()).get("propertId").toString());
            bill.setPropertyid(id2);
            bill.setConsumetype("0");//收入，删除时要写反
           r= propertyService.updateOneByBill(bill);

        } else {
            r = propertyService.updateWhenDelBill(bill);
        }

      return r;
    }

    /**
     * 插入转账记录
     *
     * @param bill
     * @return
     */
    @Override
    @Transactional
    public R travel(Bill bill) {
        System.out.println(bill.getMoney());
        int line = billMapper.insertOne(bill);
        if (line > 0) {
            JSONArray objects = JSON.parseArray(bill.getTravel());
            int id1 = Integer.parseInt(JSON.parseObject(objects.get(0).toString()).get("propertId").toString());
            bill.setPropertyid(id1);
            bill.setConsumetype("0");//支出
            propertyService.updateOneByBill(bill);

            int id2 = Integer.parseInt(JSON.parseObject(objects.get(1).toString()).get("propertId").toString());
            bill.setPropertyid(id2);
            bill.setConsumetype("1");//收入
            propertyService.updateOneByBill(bill);

            return R.sucess();
        }

        return R.fail();
    }
}